package com.qf.smsplatform.monitor.mq;


import com.qf.smsplatform.common.constants.CacheConstants;
import com.qf.smsplatform.monitor.api.CacheService;
import com.qf.smsplatform.monitor.utils.JavaMailUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import java.util.Map;
import java.util.Set;

/**
 * @Author: LeeRui
 * 2021/2/3 下午 7:29
 **/
@Component
@Slf4j
public class GatewayFeeMessageJob implements SimpleJob {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private JavaMailUtil javaMailUtil;

    @Override
    public void execute(ShardingContext shardingContext) {

        //查询全部客户信息
        Set<String> clientID = cacheService.smembers(CacheConstants.CACHE_PREFIX_CLIENT_ALL);
        //clientID
        for (String clientid : clientID) {
            //根据客户获取客户fee
            Map map = cacheService.hMGet(CacheConstants.CACHE_PREFIX_CLIENT + clientid);
            Long fee = Long.valueOf(String.valueOf(map.get("fee")));
            //判断客户余额是否小于1000
            if (fee < 1000) {  //余额不足，发送邮件通知客户充值！
                log.info("【监控模块】 余额不足，发送邮件通知客户充值！！！ fee={}", fee);
                try {
                    javaMailUtil.sendMailForFee(fee);
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }else{
                log.info("【监控模块】 客户余额充足！！！ clientid={},fee={}", clientid,fee);
            }
        }
    }
}
